<?php

include( "./admin/include/func.php");
include( "Pager/Pager.php");

$db = new db();
if ( ! $db->connect()) exit( mysql_error());

$r_id     = isset( $_REQUEST['hirdetes-sorszama']) ? (int) $_REQUEST['hirdetes-sorszama'] : 0; 
$r_delfav = isset( $_REQUEST['nem-kedvenc']) ? 1 : 0; 

if (USER::is_logged_in()) {

	if ($r_id > 0) {
		
		$db->sql = "SELECT id FROM hirdetes WHERE id='$r_id' AND aktiv=1";				
		$db->query();
		//print $db->sql;

		if (mysql_num_rows ($db->rs) > 0) {
			
			$db->sql = "SELECT id FROM kedvenc WHERE user_id='" . USER::get_id() . "' AND hird_id='$r_id'";				
			$db->query();

			if (mysql_num_rows ($db->rs) < 1) {					
				$db->sql = "INSERT INTO kedvenc (id,user_id,hird_id) VALUES ('0','" . USER::get_id() . "','$r_id')";				
				$db->query();
				//print $db->sql;
			} else if ($r_delfav > 0) {
				$db->sql = "DELETE FROM kedvenc WHERE user_id='" . USER::get_id() . "' AND hird_id='$r_id'"; 
				$db->query();
			}		
		}
	}
	
	$db->sql = "SELECT COUNT(id)
				FROM hirdetes 
				WHERE id IN (SELECT hird_id FROM kedvenc WHERE user_id=" . USER::get_id() . " ORDER BY id DESC) 
				AND aktiv=1";				
	$db->query();
		
	$rpp = 15; //row per page
	$tct = mysql_fetch_row( $db->rs);
	$tct = $tct[0];	//total count
	$pager_options = array('mode' => 'Sliding', 'perPage' => $rpp, 'delta' => 2, 'totalItems' => $tct,);
	$pager = @Pager::factory($pager_options);
	
	list($from, $to) = $pager->getOffsetByPageId();

	$db->sql = "SELECT id,nev,email,targy,szoveg,ar,telefon,weblap,kep,telepules,feladas,lejarat,
				(SELECT r.parent FROM rovat r WHERE r.id=rovat) as rovat_parent,
				(SELECT r.id FROM rovat r WHERE r.id=rovat) as rovat_id1, 
				(SELECT r.id FROM rovat r WHERE r.id=rovat_parent) as rovat_id2,
				(SELECT IF(rovat_parent=0,rovat_id1,rovat_id2)) as forovat_id,
				(SELECT IF(rovat_parent=0,0,rovat_id1)) as alrovat_id,
				(SELECT r.nev  FROM rovat r WHERE r.id=forovat_id) as forovat,
				(SELECT r.slug FROM rovat r WHERE r.id=forovat_id) as forovat_slug,
				(SELECT r.nev  FROM rovat r WHERE r.id=alrovat_id) as alrovat,
				(SELECT r.slug FROM rovat r WHERE r.id=alrovat_id) as alrovat_slug,
				(SELECT r.nev  FROM regio r WHERE r.id=regio) as regio  
				FROM hirdetes h
				WHERE h.aktiv=1 AND h.id IN (SELECT k.hird_id FROM kedvenc k WHERE k.user_id=" . USER::get_id() . " ORDER BY k.id DESC)
				ORDER BY h.szponzoralt DESC, h.lastmodified DESC
				LIMIT " . ($from - 1) . ", $rpp";
	$db->query();
	//print $db->sql;
						
	$hirdetesek = array ();
	while ($hird = mysql_fetch_assoc( $db->rs)) $hirdetesek[] = $hird;
	//debug( $hirdetesek);
}

?>

<?php include ("header.php"); ?>

<?php include ("top.php"); ?>

<?php include ("left.php"); ?>
							
<div id="middle">

<?php if (! User::is_logged_in ()) { ?>

	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<p class="hpcaption">Információ</p>
				<p class="hptitle">Kedvenc hirdetések</p>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="100px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:100px">			
				<p class="success">
					<b>A funkció használatához be kell jelentkezni!</b><br />
					Bejelentkezés után elmentheti az Önt érdeklő hirdetéseket,<br />
					hogy később bármikor könnyedén megtalálja azokat.<br />
					<b>Ha még nem regisztrált felhasználónk, akkor <a href="regisztracio.php">kattintson ide!</a></b>
				</p>			
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="100px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>

<?php }	else { ?>

<?php if (count($hirdetesek) < 1) { ?>

	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<p class="hpcaption">Információ</p>
				<p class="hptitle">Kedvencek</p>
			</div>	
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="20px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:20px">			
				<p class="success">Nem található kedvenc hirdetés.</p>			
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="20px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>
		
<?php }	else { ?>

<?php foreach ($hirdetesek as $row) {

	if ($row['kep'] != '') {
		GLOBAL $upload_path;
		$kep   = $upload_path . '/' . str_replace('-','/',$row['feladas']) . '/kep/' . $row['kep']; 
		$thumb = $upload_path . '/' . str_replace('-','/',$row['feladas']) . '/thumb/thumb_' . $row['kep'];
		if (! file_exists ($kep)) {
			$kep   = 'http://hirdetek.net/kepek/'.$row['kep'];
			$thumb = 'http://hirdetek.net/thumbs1/thumb_'.$row['kep'];
		}
	} else {
		$kep   = '#';
		$thumb = './images1/nincskep.gif'; 
	}
					
?>
	<a name="<?php print $row['id']; ?>"></a>	
	<div class="form-panel">		
		<div>
			<img src='./images1/x3a2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' /> 
			<div class="hptop">
				<div class="hplcdiv">
					<p class="hplcaption"><span>Apróhirdetés</span> sorszáma: <?php print $row['id']; ?></p>
					<p class="hprcaption">Feladó: <?php print $row['nev']; ?></p>
				</div>
				<div class="hprcdiv">
					<p class="hpltitle">Feladva: <?php print $row['feladas']; ?></p>
					<p class="hprtitle">Lejár: <?php print $row['lejarat']; ?></p>
				</div>
			</div>		
			<img src='./images1/x3b2.gif' width="21px" height="42px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x4a.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4bg.gif' width="455px" height="13px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x4b.gif' width="21px" height="13px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x5bga.gif' width="21px" height="357px" class="fleft" alt='apróhirdetés' /> 
			<div class="hpbody" style="height:357px">							
				<div>
					<a class='hppic' href="#" onclick="return(makePopup(this,'<?php print $kep; ?>'))">
						<img alt="<?php print $row['targy']; ?>" src="<?php print $thumb; ?>" />
					</a>
					<div class="hptext">
						<h1 class='targy'><?php print $row['targy']; ?></h1>						
						<textarea cols="35" rows="8" class='hirdszov' readonly="readonly"><?php print $row['szoveg']; ?></textarea>
					</div>				
				</div>
				<br />
				<hr />
				<div class="info1">
					<div class="info2">
						<table width="100%" cellspacing="3" border="0">
							<tr><td width="10%">Ár:</td><td width="90%"><?php print $row['ar']==''?'-':str_to_currency($row['ar']); ?></td></tr>
							<tr><td>Telefon:</td><td><?php print $row['telefon']==''?'-':$row['telefon']; ?></td></tr>
							<tr><td>Weblap:</td><td align="left"><?php if ($row['weblap']=='') print '-'; else { ?><a onclick="return(makePopup(this,'<?php print $row['weblap']; ?>'))">K a t t &nbsp; i d e !</a><?php } ?></td></tr>
						</table>
					</div>
					<div class="info2">
						<table width="100%" cellspacing="3" border="0">
							<tr><td width="10%">Régió:</td><td width="90%"><?php print $row['regio']; ?></td></tr>
							<tr><td>Email:</td><td><a onclick="return(makePopup(this,'<?php print 'mailto:'.$row['email']; ?>'))"><?php print $row['email'];?></a></td></tr>
							<tr><td colspan="2"><a class="kiemeles" onclick='return tovabbiInfo ( this);' href='kiemeles.php'>INGYEN HIRDETÉS KIEMELÉS</a></td></tr>
						</table>
					</div>				
				</div>
				<br />
				<hr />
				<div class="info1">
					<div class="info2">
						<table width="100%" cellspacing="3" border="0">
							<tr><td><a href="hirdetesek.php?rovat=<?php print $row['forovat_slug']; ?>"><?php print $row['forovat']; ?> &gt;&gt;</a></td></tr>
							<tr><td><a href="hirdetesek.php?felado=<?php print $row['id'] . '&amp;' . $_SERVER['QUERY_STRING']; ?>">Feladó hirdetései &gt;&gt;</a></td></tr>
							<tr><td><a class='szponz' href="kedvencek.php?nem-kedvenc&amp;hirdetes-sorszama=<?php print $row['id']; //print PAGER_QUERY_STRING; ?>">Nem kedvenc &gt;&gt;</a></td></tr>
						</table>
					</div>
					<div class="info2">
						<table width="100%" cellspacing="3" border="0">
							<tr><td><a href="<?php print $row['alrovat'] != '' ? 'hirdetesek.php?rovat=' . $row['alrovat_slug'] : ''; ?>"><?php print $row['alrovat'] != '' ? substr($row['alrovat'],0,35) . ' &gt;&gt;': '&nbsp;'; ?></a></td></tr>
							<tr><td><a onclick="return(makePopup(this,'hirdkuldes.php?id=<?php print $row['id']; ?>'))">Küldés emailben &gt;&gt;</a></td></tr>
							<tr><td><a onclick="return(makePopup(this,'nyomtatas.php?id=<?php print $row['id']; ?>'))">Nyomtatás &gt;&gt;</a></td></tr>
						</table>
					</div>				
				</div>
			</div>
			<img src='./images1/x5bgb.gif' width="21px" height="357px" class="fleft" alt='apróhirdetés' />
		</div>
		<div>
			<img src='./images1/x6a.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' /> 
			<img src='./images1/x6bg.gif' width="455px" height="34px" class="fleft" alt='apróhirdetés' /> 		
			<img src='./images1/x6b.gif' width="21px" height="34px" class="fleft" alt='apróhirdetés' />
		</div>
	</div>

	<br />
	<br />
	
<?php } ?>
 
<br />

<div id="pager"><?php echo $pager->links; ?></div>
	
<?php } ?>

<?php } ?>

<br />

</div>

<?php include ("right.php"); ?>

<?php include ("footer.php"); ?>
